<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="dictLabel"
            placeholder="请输入物料名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="treeData"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>


    <el-col :span="20" :xs="24">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="物料编码" prop="materialCode">
          <el-input
            v-model="queryParams.materialCode"
            placeholder="请输入物料编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料数量" prop="materialNum">
          <el-input
            v-model="queryParams.materialNum"
            placeholder="请输入物料数量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="规格型号" prop="materialModule">
          <el-input
            v-model="queryParams.materialModule"
            placeholder="请输入规格型号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="入库时间" prop="inTime">
          <el-date-picker clearable
                          v-model="queryParams.inTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择添加入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="库存时间" prop="outTime">
          <el-date-picker clearable
                          v-model="queryParams.outTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择修改库存时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:material:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:material:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:material:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:material:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编号" align="center" prop="goodsMaterialId" />
      <el-table-column label="物料编码" align="center" prop="materialCode" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料数量" align="center" prop="materialNum" />
      <el-table-column label="规格型号" align="center" prop="materialModule" />
      <el-table-column label="所属分类" align="center" prop="materialType">
        <template v-slot="scope">
         <dict-tag :options="dict.type.sys_material_type" :value="scope.row.materialType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否设置安全库存" align="center" prop="securityStatus">
          <template v-slot="s">
            <el-button size="mini" type="primary" v-if="s.row.securityStatus==0">是</el-button>
            <el-button size="mini" type="primary" v-if="s.row.securityStatus==1">否</el-button>
          </template>
      </el-table-column>
      <el-table-column label="物料归属部门" align="center" prop="deptName" />
      <el-table-column label="是否启用" align="center" prop="status">
          <template v-slot="s">
            <el-button size="mini" type="primary" v-if="s.row.status==0">未启用</el-button>
            <el-button size="mini" type="primary" v-if="s.row.status==1">已启用</el-button>
          </template>
      </el-table-column>
      <el-table-column label="添加入库时间" align="center" prop="inTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改库存时间" align="center" prop="outTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="userId" />
      <el-table-column label="单位" align="center" prop="unit" >
        <template v-slot="scope">
          <dict-tag :options="dict.type.sys_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:material:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:material:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
     </el-col>
    </el-row>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="uuidCode" placeholder="请输入物料编码" />
          <el-switch
            v-model="flaging"
            active-text="自动生成">
          </el-switch>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="物料数量" prop="materialNum">
          <el-input v-model="form.materialNum" placeholder="请输入物料数量" />
        </el-form-item>
        <el-form-item label="规格型号" prop="materialModule">
          <el-input v-model="form.materialModule" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="安全库存" prop="materialModule">
          <el-radio-group v-model="form.securityStatus">
            <el-radio :label="0" :value="0">是</el-radio>
            <el-radio :label="1" :value="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属分类" prop="materialType">
          <el-select v-model="form.materialType" placeholder="请输入所属分类">
            <el-option
              v-for="item in dict.type.sys_material_type"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物料归属部门" prop="deptId">
            <treeselect v-model="form.deptId"
                        :options="deptOptions"
                        :show-count="true" placeholder="请选择物料归属部门" />
        </el-form-item>
        <el-form-item label="添加入库时间" prop="inTime">
          <el-date-picker clearable
                          v-model="form.inTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择添加入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改库存时间" prop="outTime">
          <el-date-picker clearable
                          v-model="form.outTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择修改库存时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请输入单位">
            <el-option
              v-for="item in dict.type.sys_unit"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterial,
  getMaterial,
  delMaterial,
  addMaterial,
  updateMaterial ,
  selectMaterialCode ,
} from "@/api/material/manager";
import {deptTreeSelect} from "@/api/system/user";
import {getDicts} from "@/api/system/dict/data";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Material",
  dicts: ['sys_material_type', 'sys_unit'],
  watch:{
    flaging(val){
      if(val){
        this.getMaterialCode();
      }
    },
    // 根据名称筛选部门树
    dictLabel(val) {
      this.$refs.tree.filter(val);
    }
  },
  components: { Treeselect },
  data() {
    return {
      treeData: [],
      defaultProps: {
        label: function (data) {
          return data.dictLabel;
        }
      },
      // 部门树选项
      dictLabel:'',
      deptOptions: [],
      // 遮罩层
      loading: true,
      flaging:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialCode: null,
        materialName: null,
        materialNum: null,
        materialModule: null,
        materialType: null,
        securityStatus: null,
        deptId: null,
        status: null,
        inTime: null,
        outTime: null,
        userId: null,
        unit: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      uuidCode:undefined,
      rules:{

      }
    };
  },
  methods: {
    // 节点点击事件
    handleNodeClick(data) {
      this.queryParams.materialType = data.dictValue;
      this.handleQuery();
    },
    // 筛选节点
    filterNode(value, data) {
      console.log("data",data)
      if (!value) return true;
      //dictLabel跟上面筛选所绑定的v-model一致
      return data.dictLabel.indexOf(value) !== -1;
    },
    //获取左树结构数据==过查询字典表物料分类返回数据集合
    getTreeData() {
      getDicts("sys_material_type").then(r => {
        this.treeData = r.data;
      })
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询物料列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsMaterialId: null,
        materialCode: null,
        materialName: null,
        materialNum: null,
        materialModule: null,
        materialType: null,
        securityStatus: null,
        deptId: null,
        status: null,
        inTime: null,
        outTime: null,
        userId: null,
        unit: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsMaterialId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "添加物料";
      this.reset();
      this.getDeptTree();
      this.open = true;
    },
    getMaterialCode(){
      selectMaterialCode().then(r=>{
        this.uuidCode=r.msg;
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getDeptTree();
      this.uuidCode=row.materialCode;
      const goodsMaterialId = row.goodsMaterialId || this.ids
      getMaterial(goodsMaterialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsMaterialId != null) {
            updateMaterial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.materialCode=this.uuidCode;
            addMaterial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsMaterialIds = row.goodsMaterialId || this.ids;
      this.$modal.confirm('是否确认删除物料编号为"' + goodsMaterialIds + '"的数据项？').then(function() {
        return delMaterial(goodsMaterialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/material/export', {
        ...this.queryParams
      }, `material_${new Date().getTime()}.xlsx`)
    }
  },
  created() {
    this.getList();
    this.getTreeData();
  },

};
</script>
